import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/pages/enter_tab_page.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/tabbar_custom.dart';
import 'package:material_collection_app/widget/common/search_box.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

// 物资入库
class EnterPage extends StatefulWidget  {
  final Map arguments;
  EnterPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<EnterPage> with SingleTickerProviderStateMixin {

  String validStartDate;
  String validEndDate;
  String enStartDate;
  String enEndDate;
  String baseApi = 'enter/';
  String keywords = '';
  List list = [];
  List tabs = ['待接收','待清点','已清点'];
  List tabsStatus = [
    {
      'status': 'accept',
      'index': 0
    },
    {
      'status': 'check',
      'index': 1
    },
    {
      'status': 'checked',
      'index': 2
    },
  ];

  TabController _tabController;
  TextEditingController _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: tabs.length);
    // _loadData("accept");
    // _tabController.addListener((){//监听滑动或者点击
    //   if(_tabController.index ==_tabController.animation.value) {
    //     _loadData(tabsStatus[_tabController.index]['status']);
    //   }
    // });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _loadData(String status) async {
    DioUtils.request("${baseApi}getlist",
        context: context,
        method: "POST",
        params: {
          'current_page': 1,
          'status': status,
          'keywords': "",
        },
        onSuccess: (res) {
          if(res['code'] == 1) {
            setState(() {
              list = res["data"]["data"];
            });
          }else {
            Utils.showText(text: res['msg']);
          }
        },
        onError: (error) {

        }
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    List<Key> keys = [acceptKey, checkKey, checkedKey];

    return Scaffold(
      appBar: TopBar(
        height: 0,
      ),
      body: SafeArea(
        child: Container(
          color: Color(0xFF297CFF),
          child: Column(
            children: <Widget>[
              SearchBox(
                hintText: '名称',
                endDrawer: true,
                searchController: _searchController,
                onSearch: (value) {
                  setState(() {
                    keywords = value;
                  });

                  int index = _tabController.index;
                  if(index == 0) acceptKey.currentState.search();
                  if(index == 1) checkKey.currentState.search();
                  if(index == 2) checkedKey.currentState.search();
                }
              ),
              Expanded(
                child: DefaultTabController(
                  length: tabs.length,
                  initialIndex: 0,
                  child: Column(
                    children: <Widget>[
                      TabBarCustom(
                        tabs: tabs,
                        tabController: _tabController,
                      ),
                      Expanded(
                        child: TabBarView(
                          controller: _tabController,
                          children: tabsStatus.map((item) {
                            return EnterTabPage(
                              // list: list,
                              status: item['status'],
                              keywords: keywords,
                              key: keys[item['index']],
                            );
                          }).toList()
                        )
                      )
                    ]
                  )
                )
              )
            ]
          )
        )
      )
    );
  }
}